package cn.baiweigang.uu.service.futureswap;

import cn.baiweigang.uu.enums.StrategyStatusEnum;
import cn.baiweigang.uu.model.AccountInfo;
import cn.baiweigang.uu.model.PositionInfo;
import cn.baiweigang.uu.model.StrategyInfo;
import cn.baiweigang.uu.model.TradeInfo;
import cn.baiweigang.uu.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

/**
 * 基差统计服务
 */
@Service
public class BasisService {
    @Autowired
    private AccountService accountService;

    public StrategyInfo init(AccountInfo accountInfo) {
        StrategyInfo strategyInfo = new StrategyInfo();
        strategyInfo.setName(this.getClass().getSimpleName());
        strategyInfo.setStrategyHistory(new ArrayList<>());
        strategyInfo.setDateTimeTs(DateUtil.getTs());
        strategyInfo.setCurrentStrategyStatus(StrategyStatusEnum.INIT);
        strategyInfo.setMemo("新的一轮开始");

        //读取账号和持仓信息
        AccountInfo futureAccount = new AccountInfo();
        futureAccount.setSymbol(accountInfo.getSymbol());
        futureAccount.setAccountName(accountInfo.getAccountName());
        accountService.updateFutureAccountInfo(futureAccount);
        accountService.updateFuturePositionInfo(futureAccount);
        strategyInfo.setFutureAccount(futureAccount);

        AccountInfo swapAccount = new AccountInfo();
        swapAccount.setContractCode(accountInfo.getContractCode());
        swapAccount.setAccountName(accountInfo.getAccountName());
        accountService.updateSwapAccountInfo(swapAccount);
        accountService.updateSwapPositionInfo(swapAccount);
        strategyInfo.setSwapAccount(swapAccount);

        //计算开仓合约张数

        return strategyInfo;
    }

    /**
     * 检查持仓情况
     * 如果存在持仓
     *
     * @param strategyInfo
     */
    public void checkPosition(StrategyInfo strategyInfo) {
        //INIT时 检查持仓信息
        if (strategyInfo.getCurrentStrategyStatus() == StrategyStatusEnum.INIT &&
                strategyInfo.getFutureAccount().getPositionInfo() != null &&
                strategyInfo.getFutureAccount().getPositionInfo().size() > 0) {
            //把交割合约持仓转为交割订单信息
            TradeInfo futureOpen = convert2Trade(strategyInfo.getFutureAccount().getPositionInfo().get(0));
            strategyInfo.setFutureOpen(futureOpen);
//            strategyInfo.setFuturePosition(strategyInfo.getFutureAccount().getPositionInfo().get(0));
        }

        if (strategyInfo.getCurrentStrategyStatus() == StrategyStatusEnum.INIT &&
                strategyInfo.getSwapAccount().getPositionInfo() != null &&
                strategyInfo.getSwapAccount().getPositionInfo().size() > 0) {
            //把币本位永续合约持仓转为交割订单信息
            TradeInfo swapOpen = convert2Trade(strategyInfo.getSwapAccount().getPositionInfo().get(0));
            strategyInfo.setSwapOpen(swapOpen);
//            strategyInfo.setSwapPosition(strategyInfo.getSwapAccount().getPositionInfo().get(0));
        }

        if (strategyInfo.getFutureOpen() != null &&
                strategyInfo.getSwapOpen() != null) {
            strategyInfo.setCurrentStrategyStatus(StrategyStatusEnum.WAIT_TO_CLOSE);
        }
    }

    private TradeInfo convert2Trade(PositionInfo positionInfo) {
        TradeInfo tradeInfo = new TradeInfo();
        return tradeInfo;
    }
}
